import type {RuleItem, ValidateError, ValidateFieldsError} from 'async-validator'
import type {InjectionKey} from "vue";

export interface FormItemProps {
  label: string;
  prop?: string;
}

export interface FormItemRules extends RuleItem {
  trigger?: string;
}

export interface ValidateStatusProp {
  state: 'init' | 'success' | 'error';
  errorMsg: string;
  loading: boolean;
}

export interface FormValidateFailure {
  errors: ValidateError[] | null;
  fields: ValidateFieldsError;
}

export type FormRules = Record<string, FormItemRules[]>

export interface FormProps {
  model: Record<string, any>;
  rules?: FormRules;
}

export interface FormItemContext {
  prop: string;
  validate: (trigger?: string) => Promise<any>;

  resetField(): void;

  clearValidate(): void;
}

export interface FormContext extends FormProps {
  addField: (field: FormItemContext) => void;
  removeField: (field: FormItemContext) => void;
}

export interface FormInstance {
  validate: () => Promise<any>;

  resetFields:(props?:string[])=> void;

  clearValidate: (props?:string[])=> void;
}

export interface FormItemInstance {
  validateStatus: ValidateStatusProp;
  validate: () => Promise<any>;

  resetField(): void;

  clearValidate(): void;
}

export const formContextKey: InjectionKey<FormContext> = Symbol('formContextKey');
export const formItemContextKey: InjectionKey<FormItemContext> = Symbol('formItemContextKey');
